package com.itmuch.cloud.study.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.itmuch.cloud.study.service.UserService;
import com.itmuch.cloud.study.user.api.UserApi;
import com.itmuch.cloud.study.user.domain.User;
import com.itmuch.cloud.study.user.feign.IdGenFeignClient;

@RestController
public class UserController implements UserApi {
	@Autowired
	private UserService userService;
	@Autowired
	private IdGenFeignClient idGenClient;

	@PostMapping("/users")
	public User create(@RequestBody User user) {
		//获取全局的id信息
		Long id = idGenClient.genId();
		user.setId(id);
		return userService.create(user);
	}

	@GetMapping("/users/{id}")
	public User get(@PathVariable("id") String id) {
		return userService.get(id);
	}

	@PutMapping("/users/{id}")
	public User update(@PathVariable("id") String id, @RequestBody User user) {
		return userService.update(id, user);
	}

	@DeleteMapping("/users/{id}")
	public User delete(@PathVariable("id") String id) {
		return userService.delete(id);
	}

}
